home *** CD-ROM | disk | FTP | other *** search
- Path: gail.ripco.com!mambuhl
- From: mambuhl@ripco.com (Martin Ambuhl)
- Newsgroups: comp.lang.c
- Subject: Re: separate compilation
- Date: 7 Mar 1996 23:51:09 GMT
- Organization: Ripco Communications, Inc.
- Message-ID: <4hnspd$b2v@gail.ripco.com>
- NNTP-Posting-Host: golden.ripco.com
-
- D.C.Molero@dcs.warwick.ac.uk (Daniel Castillo Molero)
- in <1996Mar2.164559.24931@dcs.warwick.ac.uk> writes:
-
- >My problem is the following:
- >I had a program which creates a linked list and then prints it, and was
- >working, but when I tried to separate it in 3 different files, and compile
- >it using
- > gcc file1.c file2.c file3.c
-
- >it doesn't compile.
-
- 1) Your main() in file1.c never calls the functions in file2.c
- or file3.c, so it could never have been working even before splitting
- into separate files.
-
- 2) You need to reread your C language reference on `extern'. It does
- not mean "go search the disk for the text need to complete compilation."
- In the following revision of your code, note the use of a new file
- (called files.h) to accomplish what you are trying to do.
-
- In the following revision of your code, my changes and comments are, as
- usual, marked with `/* mha - ... */'.
-
-
- /* mha - files.h (new file) */
- struct side {
- int a;
- struct side *next;
- };
-
- /* mha - file1.c */
- #include <stdio.h>
- #include <stdlib.h>
- #include "files.h" /* mha - added */
-
- /* mha - removed useless `extern struct side;' */
-
- int main(void)
- {
- struct side *tmp;
-
- /* first_side is declared in file2.c and points to the first
- * element of the list. */
- /* mha - well, it had function scope in file1.c and was invisible
- * to the following declaration */
- extern struct side *first_side;
- /* print the three elements */
-
- /* mha - note that the following, without at least initializing tmp
- * to be NULL, may well result in a segfault (or equivalent) */
- for (tmp = first_side; tmp; tmp = tmp->next)
- printf("side: %d\n", tmp->a);
-
- return 0;
- }
-
- /* mha - file2.c */
- #include <stdlib.h> /* mha - added. Without this malloc()
- * has an implicit declaration that,
- * among other things, says malloc()
- * returns an `int' rather than a `void
- * *' */
- #include "files.h" /* mha - added (definition of struct
- * side move to files.h) */
- struct side *first_side; /* mha - moved from within
- * `create_list()', where it had
- * function scope and was not visible
- * to file1.c */
-
- void store_side(struct side *, struct side **); /* mha - removed useless
- * variable names
- * [optional] */
-
- void create_list(void)
- {
- struct side *i;
- /* mha - moved `struct side *first_side;' out of function scope */
- /* mha - I have _not_ checked to see what this code actually does.
- * I doubt that it does what you want, but that's up to you. */
-
- /* insert first element */
- first_side = 0;
- i = malloc(1 * sizeof(struct side));
- i->a = 0;
- store_side(i, &first_side);
-
- /* insert second element */
- i = malloc(1 * sizeof(struct side));
- i->a = 1;
- store_side(i, &first_side);
-
- /* insert third elemenet */
- i = malloc(1 * sizeof(struct side));
- i->a = 2;
- store_side(i, &first_side);
-
- }
-
- /* mha - file3.c */
- #include "files.h" /* mha - added */
-
- /* mha - removed useless `extern struct side;' */
-
- /* insert a new element in the list */
- /* mha - changed `first_side' to `s'. Using the name of a global
- * variable as the name of a parameter can confuse humans. */
- void store_side(struct side * i, struct side ** s)
- {
- i->next = *s;
- *s = i;
- }
-
- --
- * Martin Ambuhl net: mambuhl@ripco.com
- * Chicago, IL (USA)
-